using Config;
using Entity.Monster.State;
using Entity.Player;
using MoonFramework.FSM;
using MoonFramework.Scripts.Mono;
using MoonFramework.Scripts.Pool;
using MoonFramework.Scripts.Tool.System.Setting;
using Tool.Name;
using UnityEngine;

namespace Entity.Monster
{
    public class Spider : Monster
    {
        public override void Init(ActorViewController viewController)
        {
            base.Init(viewController);
            
            //初始化Config
            ActorConfig = ConfigManager.Instance.GetConfig<MonsterConfig>(ConfigName.Monster, nameof(Spider));
            MonsterConfig = (MonsterConfig)ActorConfig;
            
            //初始化动画
            viewController._AttackAnimation = AnimationConfig.SpiderAttack;
            viewController._IdleAnimation = AnimationConfig.SpiderIdle;
            viewController._RunAnimation = AnimationConfig.SpiderRun;
            viewController._DeadAnimation = AnimationConfig.SpiderDead;
            viewController._WalkAnimation = AnimationConfig.SpiderWalk;
            viewController._HurtAnimation = AnimationConfig.SpiderHurt;
            
            fsmMachine = ObjPoolManager.Instance.Pop(()=> new MonsterFsm(), 
                res=> res.Init(nameof(MonsterIdleState), this));
            MonoManager.Instance.SendUpdateEvent(OnUpdate);
        }

        private void OnUpdate()
        {
            var pos1 = viewController.transform.position;
            var pos2 = PlayerController.Instance.Player.viewController.transform.position;
            if (Vector3.Distance(pos1, pos2) > ((SpiderViewController)viewController).attackDistance)
                fsmMachine.ChangeState(nameof(SpiderIdleState));
            else
                fsmMachine.ChangeState(nameof(SpiderRunState));
        }
    }
}